package psn.zjk.exporter;

import psn.zjk.exporter.utils.Assert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author sdyt_zhangjunkai
 * @version 1.0
 * @CLASS_NAME DynamicTableBuilder
 * @DESCRIPTION
 * @CREATE_TIME 2020/9/16 14:14
 */
public class FixedRowHeaderTableBuilder extends ExcelDataBuilder {

    /**
     * @METHOD_NAME: ExcelDataBuilder
     * @DESCRIPTION: 固定行头
     * @CREATE_TIME 2020/9/4 14:45
     * @RETURN
     * @AUTHOR sdyt_zhangjunkai
     */
    protected FixedRowHeaderTableBuilder(List<String> fixedRowHeaders, String headerField) {
        super();
        Assert.hasLength(headerField, "headerField is empty!");
        Assert.notEmpty(fixedRowHeaders, "fixedRowHeaders is null!");
        //列头
        excelData.fixedRowHeaders = fixedRowHeaders;
        //行头
        excelData.headerField = headerField;
    }

    protected FixedRowHeaderTableBuilder(List<String> fixedRowHeaders, String headerField, String valueField) {
        super();
        Assert.hasLength(headerField, "headerField is empty!");
        Assert.notEmpty(fixedRowHeaders, "fixedRowHeaders is null!");
        //列头
        excelData.fixedRowHeaders = fixedRowHeaders;
        //行头
        excelData.headerField = headerField;
        excelData.valueField = valueField;
    }


    @Override
    protected void assembleDatas() {
        SheetRow firstRow = row(excelData.firstRow);

        int column = excelData.firstDataColumnIndex;
        SheetColumn sheetColumn = null;
        for (Object data : excelData.datas) {// 一条数据一列
            // 列头取值
            if (excelData.headerField == null) {
                sheetColumn = column(column++);
            } else {
                String columnValue = getPropertyWithConvertNull(data, excelData.headerField).toString();
                sheetColumn = uniqueHeaders.get(columnValue);
            }

            for (SheetRow sheetRow : excelData.sheetRows.values()) {
                if (sheetRow.getHeader() == null) {
                    continue;
                }
                Object value = getMappedValue(data, sheetRow.getHeader());
                cell(sheetRow, sheetColumn, value);
            }
        }
    }

    @Override
    protected void assembleTmpPojo(SheetColumn column, Object pojo) {
        for (SheetRow row : excelData.sheetRows.values()) {
            Object value = getMappedValue(pojo, row.getHeader());
            cell(row, column, value);
        }
    }

    @Override
    protected List<String> getHeaders() {
        List<String> list = null;
        if (mergeRepeat) {
            HashSet<String> headers = new HashSet<>();
            excelData.datas.forEach(o -> {
                String header = getPropertyWithConvertNull(o, excelData.headerField).toString();
                headers.add(header);
            });
            list = new ArrayList<>(headers);
        } else {
            list = new ArrayList<>();
            for (Object data : excelData.datas) {
                String header = getPropertyWithConvertNull(data, excelData.headerField).toString();
                list.add(header);
            }
        }
        return list;
    }

    @Override
    protected List<String> getRowHeaders() {
        return excelData.fixedRowHeaders;
    }

}
